package form2;

import FMS.Form4;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import FMS.Form2;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;



public class EditForm2 extends WebPage {
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	
	private Model promissoryNoModel = new Model();
	private Model nameModel = new Model();
	private Model dateModel = new Model();
	private Model amountModel = new Model();
	private Model semesterModel = new Model();
	private Model syModel = new Model();
	private Model dueDateModel = new Model();
	private Model courseModel = new Model();
	private Model yearModel = new Model();
	private Model witnessModel = new Model();
	//private Model statusModel = new Model();
	//private DropDownChoice status; //sa president view ra ni dapat makita
	//private List choice = Arrays.asList(new String[] { "PENDING", "APPROVED" });
	private DropDownChoice year; //sa president view ra ni dapat makita
	private List choice = Arrays.asList(new String[] { "1", "2", "3", "4", "5" });
	
	public EditForm2() throws IOException, SQLException {
			
		Form form = new Form("editForm2");
			
		TextField promissoryNo = new TextField("promissoryNo", promissoryNoModel);
		promissoryNo.setRequired(true);
		form.add(promissoryNo);
		
		TextField name = new TextField("name", nameModel);
		name.setRequired(true);
		form.add(name);
		
		TextField date = new TextField("date", dateModel);
		date.setRequired(true);
		form.add(date);
		
		TextField amount = new TextField("amount", amountModel);
		amount.setRequired(true);
		form.add(amount);
		
		TextField semester = new TextField("semester", semesterModel);
		semester.setRequired(true);
		form.add(semester);
		
		TextField sy = new TextField("sy", syModel);
		sy.setRequired(true);
		form.add(sy);
		
		TextField dueDate = new TextField("dueDate", dueDateModel);
		dueDate.setRequired(true);
		form.add(dueDate);
		
		TextField course = new TextField("course", courseModel);
		course.setRequired(true);
		form.add(course);
		
		year = new DropDownChoice("year", yearModel, choice);
		form.add(year);
		
		
		TextField witness = new TextField("witness", witnessModel);
		witness.setRequired(true);
		form.add(witness);

		//status = new DropDownChoice("status", statusModel, choice);
		//form.add(status);
		
		form.add(new Button("add"){
			
			private static final long serialVersionUID = 1L;
		
				public void onSubmit(){
				
					try {
				
						String promissoryNo = (String)promissoryNoModel.getObject();
						String name = (String)nameModel.getObject();
						String date = (String)dateModel.getObject();
						String amount = (String)amountModel.getObject();
						String semester = (String)semesterModel.getObject();
						String sy = (String)syModel.getObject();
						String dueDate = (String)dueDateModel.getObject();
					    String course = (String)courseModel.getObject();
	                    String year = (String)yearModel.getObject();
	                    String witness = (String)witnessModel.getObject();
						//String status = (String)statusModel.getObject();
	                    
						Form2 form2 = new Form2();
						
						form2.setPromissoryNo(Integer.parseInt(promissoryNo));
						form2.setName(name);	
						form2.setDate(date);
						form2.setAmount(Integer.parseInt(amount));
						form2.setSemester(Integer.parseInt(semester));
						form2.setSy(sy);
						form2.setDueDate(dueDate);
						form2.setCourse(course);
						form2.setYear(Integer.parseInt(year));
						form2.setWitness(witness);
						//form2.setStatus(status);
												
						
						sqlMap.insert("Form2.insertPromissory", form2);
						
						//ViewForm2 page = new ViewForm2();
						//setResponsePage(page);
						//error("added");
					}
					catch (Exception exception){
					 	error("Fill up necessary fields");
				 	        exception.printStackTrace();}
				//}catch (IOException ioException) {
					//ioException.printStackTrace(); }
					
				}}); 
					
		//add(new FeedbackPanel("error"));
				
		add(form);
	
   }

		
		}

